<template>
	<view class="content">
		<navigator v-if="tabCurrentIndex==0" url="/pages/address/address?source=1" class="address-section">
			<view class="order-content" v-if="addressData.name">
				<text class="iconfont icon-location-fill"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text>
					</view>
					<text class="address">{{addressData.province}}{{addressData.city}}{{addressData.area}}{{addressData.street}}</text>
				</view>
				<text class="iconfont icon-right"></text>
			</view>
			<view class="order-content" v-if="!addressData.name">
				<text class="iconfont icon-location-fill"></text>
				<view class="cen">
					<view class="top">
						<!-- <text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text> -->
						<text>请选择地址</text>
					</view>
					<!-- <text class="address">{{addressData.street}}</text> -->
				</view>
				<text class="iconfont icon-right"></text>
			</view>
			<image class="a-bg" src=""></image>
		</navigator>
		<navigator v-if="tabCurrentIndex==1" url="/pages/merchant/list" class="address-section">
			<view class="order-content" v-if="merchantData.merchantUuid">
				<text class="iconfont icon-location-fill"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{merchantData.merchantName}}</text>
					</view>
					<text class="address">{{merchantData.merchantAddress}}公里</text>
				</view>
				<text class="iconfont icon-right"></text>
			</view>
			<view class="order-content" v-if="!merchantData.merchantUuid">
				<text class="iconfont icon-location-fill"></text>
				<view class="cen">
					<view class="top">
						<!-- <text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text> -->
						<text>请选择门店</text>
					</view>
					<!-- <text class="address">{{addressData.street}}</text> -->
				</view>
				<text class="iconfont icon-right"></text>
			</view>
		</navigator>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view>
			<view class="g-item">
				<image :src="product.productMainImage.url"></image>
				<view class="right">
					<text class="title clamp">{{product.productName}}</text>
					<text class="spec" v-if="product.skuEnabled">
						<text v-for="sku in productSku.skuAttrValueList">{{sku.skuAttrValue}}</text>
					</text>
					<text class="spec" v-if="!product.skuEnabled">-</text>
					<view class="price-box">
						<text class="price" v-if="product.skuEnabled">{{productSku.skuUnitPoint}}</text>
						<text class="price" v-if="!product.skuEnabled">{{product.unitPoint}}</text>
						<text class="number">x {{unit}}</text>
					</view>
				</view>
			</view>
		</view>
		</view>
		<!-- 金额明细 -->
		<view class="amount-list">
			<view class="amount-list-cell b-b">
				<text class="cell-tit clamp">商品积分</text>
				<text class="cell-tip red">{{productPoint}}积分</text>
			</view>
			<view class="amount-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip" v-if="tabCurrentIndex==0">￥{{freightAmount}}</text>
				<text class="cell-tip" v-if="tabCurrentIndex==1">￥0</text>
			</view>
			<view class="amount-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="memo" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{actualAmount}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	export default {
		data() {
			return {
				tabCurrentIndex:0,
				navList: [{
						state: '1',
						text: '快递配送'
					}],
				product:{},
				unit: 1,	//商品数量
				productPoint: 0, //商品积分
				productAmount: 0.00, //商品金额, 未扣减任何优惠
				deductAmount: 0.00, //优惠金额
				actualAmount: 0.00, //实付金额
				freightAmount: 0.00, //运费
				addressData: {}, //收货地址
				merchantData: {}, //门店地址
				maskState: 0, //优惠券面板显示状态
				memo: '', //备注
				type: ''
			}
		},
		components: {
			uniPopup
		},
		onLoad(option) {
			//商品数据
			this.productId = option.productId;
			this.productSkuId = option.productSkuId;
			this.unit = option.unit
			this.inquiryPointProductById(this.productId);
			this.inquiryDefaultAddress(this.userInfo.userUuid);
			//如果开启了门店
			if(this.applicationConfig.applicationMerchantEnabled){
				this.navList.push({
						state: '2',
						text: '门店自提'
					})
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'applicationConfig'])
		},
		watch: {
			addressData(val) {
				this.calculatePointFreightAmount(val.province, val.city, val.area);
			}
		},
		methods: {
			tabClick(index) {
				this.tabCurrentIndex = index;
				if(index==1){
					this.actualAmount = this.productAmount - this.deductAmount;
				}else if(index==0){
					this.actualAmount = this.productAmount - this.deductAmount + this.freightAmount;
				}
			},
			//查询积分商品
			inquiryPointProductById(productId) {
				let that = this;
				let postData = {
					productUuid: productId
				}
				this.$api.request.pointGoodsDetail(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.product = res.body.data;
						if(this.productSkuId){
							this.product.skuList.forEach(function(val,index){
								if(that.productSkuId === val.productSkuUuid){
									that.productSku = val;
								}
							})
						}
						if(!this.product.skuEnabled)
							that.productPoint = (that.product.unitPoint * this.unit);
						else
							that.productPoint = (that.productSku.skuUnitPoint * this.unit);
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			//查询默认收货地址
			inquiryDefaultAddress(userUuid) {
				let that = this;
				let postData = {
					userDTO: {
						userUuid: userUuid
					}
				}
				this.$api.request.userShip(postData, res => {
					if (res.body.status.statusCode === '0') {
						res.body.data.addresses.forEach(function(val, index) {
							if (val.default) {
								that.addressData = val;
							}
						})
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			toActualAmount(productAmount, deductAmount, freightAmount){
				return productAmount - deductAmount + freightAmount;
			},
			// 计算运费金额
			calculatePointFreightAmount(province, city, area) {
				let postData = {
					productDTO: {
						productUuid: this.productId
					},
					productSkuDTO: {
						productSkuUuid: this.productSkuId
					},
					orderDTO: {
						productUnit: this.unit,
						deliveryProvince: province,
						deliveryCity: city,
						deliveryArea: area
					}
					
				}
				this.$api.request.calculatePointFreightAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.freightAmount = res.body.data.orderDTO.freightAmount;
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			submit() {
				//检查收货地址是否选择
				if (this.tabCurrentIndex==0&&!this.addressData.userDeliveryAddressUuid) {
					this.$api.msg('未选择收货地址');
					return;
				}
				if (this.tabCurrentIndex==1&&!this.merchantData.merchantUuid) {
					this.$api.msg('未选择提货门店');
					return;
				}
				//post订单数据到后台
				let postData = {
					actionType: 'ADD',
					orderDTO:{
						userDTO: {
							userUuid: this.userInfo.userUuid
						},
						productUnit: this.unit,
						deliveryAddressDTO: {
							userDeliveryAddressUuid: this.addressData.userDeliveryAddressUuid
						},
						deliveryType: this.tabCurrentIndex==0?'1':'2'
					},
					productDTO:{
						productUuid: this.productId
					},
					productSkuDTO:{
						productSkuUuid: this.productSkuId
					}
				}
				this.$api.request.createPointOrder(postData, res => {
					if (res.body.status.statusCode === '0') {
						if (res.body.status.statusCode === '0') {
							//如果需要支付余额
							if(this.actualAmount>0){
								uni.redirectTo({
									url: '/pages/point/point-pay?orderNo=' + res.body.data.orderNo
								})
							}
							//如果不需要支付余额
							else{
								uni.redirectTo({
									url: '/pages/point/point-pay-success'
								})
							}
							
						} else {
							this.$api.msg(res.body.status.errorDesc);
						}
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});

			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page{
		background: $bg-color-light;
	}
	.content{
		margin: 0 $page-row-spacing;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-location-fill{
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}
	
	.store{
		display: flex;
		flex-direction: row;
		margin: 40upx 30upx 0 30upx;
		font-size: $font-sm;
		color: $font-color-dark;
		.portrait{
			width: 40upx;
			height: 40upx;
			margin-right: 10upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;


		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
					font-size: $font-base;
					color: $font-color-dark;
					font-weight: bold;
					&:after {
						content: '积分';
						font-size: $font-base;
					}
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.amount-list {
		margin-top: 16upx;
		background: #fff;
	}

	.amount-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: $font-lg;
			font-weight: bold;
			color: $font-color-dark;
			
			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $font-color-dark;
			}

			&.red {
				font-weight: bold;
				color: $font-color-dark;
			}

			& .close {
				color: $font-color-light;
				margin-left: 20upx;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}
	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $font-color-base;
			margin-left: 8upx;
		}

		.price {
			font-size: $font-lg;
			font-weight: bold;
			color: $font-color-dark;
			
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}
</style>
